package com.encapi.framework.wrapper.request;

import com.encapi.framework.core.CryptManager;
import com.encapi.framework.exception.DecryptBodyException;
import com.encapi.framework.exception.ExceptionUtil;
import com.encapi.framework.wrapper.RequestDataWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import javax.annotation.Resource;

public class DefaultRequestDataWrapper implements RequestDataWrapper {

    // 日志
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private CryptManager cryptManager;

    @Override
    public String unwrap(String body, MethodParameter method, HttpHeaders headers) {
        // 开启调试
        if (log.isDebugEnabled()) {
            log.debug("[encapi] request body before decrypted： {}", body);
        }

        // 解密参数
        String decryptStrBody = cryptManager.decrypt(body, null, headers);

        // 开启调试
        if (log.isDebugEnabled()) {
            log.debug("[encapi] request body after decrypted： {}", decryptStrBody);
        }

        return decryptStrBody;
    }
}
